import moment from 'moment';
import { mapGetters } from 'vuex';
export default {
	props: {
		userRole: { type: String, default: '' },
		queryBtn: { type: String, default: '' },
		dataType: { type: String, default: '' }
	},
	data() {
		return {
			loading: false,
			queryData: { page: 1, limit: 15 },
			dataCount: 0,
			tableData: [],
			checkedData: []
		}
	},
	computed: {
		...mapGetters(['userInfo', 'siteConfig', 'principalData']),
		researchData() {
			return this.siteConfig.filter(n => n.type === '1');
		},
		measuringData() {
			return this.siteConfig.filter(n => n.type === '2');
		},
		isFixed() {
			return this.dataCount > 0 ? 'right' : false;
		}
	},
	mounted() {
		this.queryTableData();
	},
	methods: {
		//查询数据
		queryTableData() {
			this.loading = true;
			this.getTableData();
		},

		//选择数据
		handleSelectionChange(arr) {
			this.checkedData = [...arr];
		},

		//删除数据
		handleDeleteData(id) {
			let that = this;
			that.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await that.deleteDataRequest(id);
						instance.confirmButtonLoading = false;
					}
					done();
				}
			}).catch(() => {});
		},

		//批量删除数据
		handleBacthDelete() {
			let that = this;
			if (this.checkedData.length === 0) {
				this.$showCustomMessage('warning', '请先选择需要删除的数据！');
				return false;
			}
			that.$confirm('此操作将批量删除数据, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
				async beforeClose(action, instance, done) {
					if (action === 'confirm') {
						instance.confirmButtonLoading = true;
						await that.deleteDataRequest();
						instance.confirmButtonLoading = false;
					}
					done();
				}
			}).catch(() => {});
		},

		//格式化时间
		timeFormatter(row) {
			return !row.auditTime ? '--' : moment(row.auditTime).format('YYYY-MM-DD');
		},

		//课题状态格式化
		statusFormatter(row) {
			let statusAttrs = { size: 'medium' };
			let statusData = this.$getDataStatus(row);
			statusAttrs['type'] = statusData.type;
			return this.$createElement('elTag', { attrs: statusAttrs }, statusData.text);
		},

		//格式化课题名称
		topicNameFormatter(data) {
			if (data.topic && data.topic.name) {
				return data.topic.name;
			} else {
				return '--'
			}
		},

		//格式化测量数据
		measureFormatter(data) {
			let datas = data.measureList || [];
			let dataArr = datas.map(item => item.piId);
			return dataArr.join();
		},

		//格式化申请机时
		applyHourFormmatter(data) {
			return !data.machineHour ? '--' : data.machineHour;
		},

		//格式化剩余时间
		surplusHourFormatter(data) {
			let useHour = !data.userHour ? 0 : Number(data.userHour);
			let machineHour = !data.machineHour ? 0 : Number(data.machineHour);
			return machineHour > 0 ? machineHour - useHour : '--'
		},

		//格式化PI数据
		piListFormatter(data) {
			let datas = data.piList || [];
			let dataArr = datas.map(item => {
				return item.pkName;
			});
			return dataArr.join();
		},

		//格式化PI姓名
		piNameFormatter(data) {
			return !data.topic.pkName1 ? '--' : data.topic.pkName1;
		},
		//格式化PI姓名
		piNameFormatter2(data) {
			return !data.topic.pkName1 ? '--' : data.pkName;
		},

		//格式化有效期
		lifeSpanFormatter(data) {
			return !data.lifespan ? '--' : data.lifespan;
		},

		selectableChecked(row, index) {
			return row.isSave === '2';
		},

		//课题详情
		handleLookData(id) {
			let query = { id };
			let path = `/${this.userRole}/${this.dataType}/detail`;
			this.$router.push({ path, query });
		},

		//课题表单
		handleOpenForm(id) {
			let query = { id };
			let path = `/${this.userRole}/${this.dataType}/form`;
			this.$router.push({ path, query });
		},

		handleAuditData(id) {
			let query = { id };
			let path = `/${this.userRole}/${this.dataType}/audit`
			this.$router.push({ path, query });
		},

		handleManageEdit(id) {
			let query = { id };
			let path = `/${this.userRole}/${this.dataType}/form`;
			this.$router.push({ path, query });
		},

		//编辑成果
		handleEditAchievement(id) {
			let query = { id };
			let path = `/${this.userRole}/${this.dataType}/edit`;
			this.$router.push({ path, query });
		}
	}
}